package LEETCODE2023.BISHUA100;

public class SolutionBM33 {
    /**
     * 自己的解法
     * @param pRoot
     * @return
     */
    public TreeNode Mirror(TreeNode pRoot) {
        if (pRoot == null) {
            return null;
        }
        // write code here
        if (pRoot.left == null && pRoot.right == null) {
            return pRoot;
        }
        TreeNode temp = pRoot.left;
        pRoot.left = pRoot.right;
        pRoot.right = temp;
        if (pRoot.left != null) {
            Mirror(pRoot.left);
        }
        if (pRoot.right != null) {
            Mirror(pRoot.right);
        }
        return pRoot;
    }

    /**
     * 官方解法
     * @param pRoot
     * @return
     */
    public TreeNode Mirror1(TreeNode pRoot) {
        if (pRoot == null) {
            return null;
        }
        TreeNode left = Mirror1(pRoot.left);
        TreeNode right = Mirror1(pRoot.right);
        pRoot.left = right;
        pRoot.right = left;
        return pRoot;
    }
}
